package com.ctl.model;

import lombok.Getter;
import lombok.Setter;

import java.io.*;
import java.util.UUID;

@Getter
@Setter
public class User implements Serializable {
    private String uid;
    private String name;
    private Integer age;
    private Character sex;
    private String address;
    private String city;
    private String money;

    public User() {
        this.uid= UUID.randomUUID().toString().toUpperCase();
        this.name="ctl";
        this.age=30;
        this.sex='M';
        this.address="XX";
    }

    public User(String name) {
        this.name = name;
    }

    public User(String name, Character sex) {
        this.name = name;
        this.sex = sex;
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public User(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public User(String name, Integer age, Character sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public User(String uid, String name, Integer age, Character sex, String address) {
        this.uid = uid;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid='" + uid + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", address='" + address + '\'' +
                '}';
    }
    //不写 变更字段后反序列化失败 写了serialVersionUID 字段变更后依旧可以反序列化成功
//    private static final long serialVersionUID = 1000L;
    public static void main(String[] args) throws IOException, ClassNotFoundException {
       // -1647152930695584146, local class serialVersionUID = 1000
        User user = new User();
        user.setUid(UUID.randomUUID().toString());
        user.setAge(30);
        String filePath="/home/ctl/output.txt";

//        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));
//        objectOutputStream.writeObject("string");
//        objectOutputStream.writeObject(user);
//        objectOutputStream.close();

        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
        String string = (String) objectInputStream.readObject();
        User userS = (User) objectInputStream.readObject();
        objectInputStream.close();
        System.out.println(string + ", age: " + userS.getAge() + ", uid: " + userS.getUid());
    }
}
